<template>
	<view>
		<view class="navBar">
			<view class="left">
				<uni-icons type="left" size="24" @click="toBack"></uni-icons>
			</view>
			<view class="center">用户登录</view>
			<view class="right"></view>
		</view>
		<view class="headText">
			<view class="register-cn">登录</view>
			<view class="register-en">please login to your accounts</view>
		</view>
		<view class="registerBox">
			<view class="userInput">
				<view class="">用户名:</view>
				<input type="text" v-model="user.username" placeholder="请输入用户名" />
			</view>
			<view class="passwordInput">
				<view class="">密码:</view>
				<input type="password" v-model="user.password" placeholder="请输入密码" />
			</view>
			<button class="login-btn" type="primary" @click="login">登录</button>
			<button class="register-btn" type="default" @click="toRegister">去注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					username: '',
					password: '',
				}
			}
		},
		methods: {
			toRegister() {
				uni.navigateTo({
					url: '/pages/register/register',
				});
			},
			toBack() {
				uni.navigateBack({
					delta: 1,
				});
			},
			toIndex() {
				uni.reLaunch({
					url: "/pages/index/index"
				})
			},
			login() {
				uni.request({
					url: 'http://localhost:8080/login',
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					data: {
						username: this.user.username,
						password: this.user.password,
					},
					success: res => {
						console.log(res.data);
						if (res.data.code == 200) {
							this.getToken();
						} else {
							uni.showToast({
								icon: "error",
								title: "登录失败"
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			getToken() {
				uni.request({
					url: 'http://localhost:8080/getToken',
					method: 'POST', // 使用 POST 请求方法
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',            // 设置请求头dxz
					},
					data: {
						username: this.user.username,
					},
					success: res => {
						console.log(res.data.data);
						if (res.data.code === 200) {
							uni.setStorageSync("token", res.data.data); // 保存后端返回的token数据
							this.toIndex();
						} else {
							uni.showToast({
								icon: "error",
								title: "获取token失败",   // 显示错误信息
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
		}
	}
</script>



<style lang="scss">
	.navBar {
		display: flex;
		height: 100rpx;
		background-color: white;
		align-items: center;
		padding: 0 30rpx;
		/* #ifdef MP-WEIXIN */
		padding-top: 100rpx;        // 微信小程序顶部导航栏高度
		/* #endif */

		.left {
			flex: 1;
		}

		.center {
			flex: 1;
			color: #646566;
			text-align: center;
			font-size: 34rpx;
			font-weight: 700;
		}

		.right {
			flex: 1;
		}
	}

	.headText {
		margin: 140rpx 30rpx;

		.register-cn {
			margin-bottom: 30rpx;
			font-size: 52rpx;
			color: #646566;
			font-weight: 700;
		}

		.register-en {
			font-size: 38rpx;
			font-weight: 400;
			color: #83838b;
		}

	}

	.registerBox {
		margin: 0 50rpx;

		.userInput,
		.passwordInput {
			display: flex;
			align-items: center;
			padding: 30rpx 0;
			border-bottom: 1rpx solid #ccc;

			view {
				flex: 1;
			}

			input {
				flex: 3;
			}
		}


		.register-btn {
			margin-top: 100rpx;
			border-radius: 50rpx;
			border: 2rpx solid #ccc;
		}

		.login-btn {
			margin-top: 50rpx;
			border-radius: 50rpx;
		}


	}
</style>